import pandas as pd
from pulp import *

def calculateDdishes():
    # 读取 Excel 文件并创建 Pandas 数据帧
    df = pd.read_excel('食谱数据.xlsx')

    # 将食品名称作为索引，并将价格转换为字典
    prices = df.set_index('食物名称')['价格'].to_dict()

    # 创建 PuLP 线性规划模型
    model = LpProblem("最小化食品总价", LpMinimize)

    # 创建变量（每个食品是否选择）
    food_vars = LpVariable.dicts("Food", df['食物名称'], 0, 1, LpBinary)
    # 添加约束
    model += lpSum(prices[f] * food_vars[f] for f in df['食物名称'])  # 目标函数（价格最小化）

    for cat in df['品类'].unique():
        model += lpSum(df.loc[df['品类'] == cat]['热量（千卡）'] * food_vars[f] for f in df['食物名称']) >= 1750  # 热量约束
        model += lpSum(df.loc[df['品类'] == cat]['蛋白质(克)'] * food_vars[f] for f in df['食物名称']) >= 65  # 蛋白质约束
        model += lpSum(df.loc[df['品类'] == cat]['维生素C(毫克)'] * food_vars[f] for f in df['食物名称']) >= 80  # 维生素C约束
        model += lpSum(df.loc[df['品类'] == cat]['维生素E(毫克)'] * food_vars[f] for f in df['食物名称']) >= 10  # 维生素E约束
        model += lpSum(df.loc[df['品类'] == cat]['钙(毫克)'] * food_vars[f] for f in df['食物名称']) >= 1100  # 钙(毫克)约束
        model += lpSum(df.loc[df['品类'] == cat]['铁(毫克)'] * food_vars[f] for f in df['食物名称']) >= 10  # 铁(毫克)约束
        model += lpSum(food_vars[f] for f in df.loc[df['品类'] == cat]['食物名称']) >= 5  # 每个品类至少选择多少个食品

    # 求解模型
    model.solve()

    # 输出最优解
    print("所选择的食品组合是：")
    for f in df['食物名称']:
        if food_vars[f].value() == 1:
            print(f)

    print("总价：", value(model.objective))

